#include <pthread.h>
#include <stdio.h>

volatile int another_shared = 0;

void *thread_run(void *args)
{
    int *calculator = (int *)args;                 // 传递过来的参数
    printf("hello thread[%ld]\n", pthread_self()); // 获取当前线程id
    for (int i = 0; i < 1000; i++)
    {
        (*calculator)++; // 注意：对于取指针数据操作，一定要先解引用后++
        another_shared++;
    }
}

int main(int argc, char const *argv[])
{
    int calculator;
    pthread_t tid1;
    pthread_t tid2;

    pthread_create(&tid1, NULL, thread_run, &calculator);
    pthread_create(&tid2, NULL, thread_run, &calculator);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    printf("calculator is %d\n", calculator);
    printf("another_shared is %d\n", another_shared);
    return 0;
}
